package one.stand.controller;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.jmp.dto.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import one.stand.common.cache.PublicBaseCacheModel;
import one.stand.common.cache.PvCacheModel;
import one.stand.common.cache.TicketOrderSumCacheModel;
import one.stand.dto.ConvertUtils;
import one.stand.dto.UserDto;
import one.stand.mapper.GoodsMapper;
import one.stand.mapper.SeeRecordApiMessageMapper;
import one.stand.model.CrmSeeRecordModel;
import one.stand.model.Goods;
import one.stand.service.user.UserAdminService;
import one.stand.util.RedisUtil;
import one.stand.util.SpecialSymbolsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

@Api(description = "数据大屏")
@RestController
@RequestMapping("datav")
@Slf4j
public class DatavController {

    @Autowired
    private UserAdminService userAdminService;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private SeeRecordApiMessageMapper seeRecordApiMessageMapper;

    private List<DatavMapNewResponse> default_list = JSON.parseArray("[" +
            "{" +
            "\"lat\": 25.82," +
            "\"lng\": 98.86," +
            "\"value\": 17" +
            "}," +
            "{" +
            "\"lat\": 26.65," +
            "\"lng\": 118.186," +
            "\"value\": 17" +
            "}," +
            "{" +
            "\"lat\": 46.09," +
            "\"lng\": 122.04," +
            "\"value\": 19" +
            "}," +
            "{" +
            "\"lat\": 28.41," +
            "\"lng\": 121.27," +
            "\"value\": 20" +
            "}," +
            "{" +
            "\"lat\": 28.01," +
            "\"lng\": 120.39," +
            "\"value\": 20" +
            "}," +
            "{" +
            "\"lat\": 24.7," +
            "\"lng\": 108.09," +
            "\"value\": 20" +
            "}," +
            "{" +
            "\"lat\": 26.26," +
            "\"lng\": 105.95," +
            "\"value\": 20" +
            "}," +
            "{" +
            "\"lat\": 28.27," +
            "\"lng\": 119.54," +
            "\"value\": 21" +
            "}," +
            "{" +
            "\"lat\": 39.66," +
            "\"lng\": 106.8," +
            "\"value\": 21" +
            "}," +
            "{" +
            "\"lat\": 22.38," +
            "\"lng\": 107.37," +
            "\"value\": 22" +
            "}," +
            "{" +
            "\"lat\": 26.67," +
            "\"lng\": 119.56," +
            "\"value\": 22" +
            "}," +
            "{" +
            "\"lat\": 25.1," +
            "\"lng\": 104.91," +
            "\"value\": 22" +
            "}," +
            "{" +
            "\"lat\": 23.43," +
            "\"lng\": 114.41," +
            "\"value\": 23" +
            "}," +
            "{" +
            "\"lat\": 21.62," +
            "\"lng\": 108.34," +
            "\"value\": 26" +
            "}," +
            "{" +
            "\"lat\": 42.03," +
            "\"lng\": 121.68," +
            "\"value\": 26" +
            "}," +
            "{" +
            "\"lat\": 24.48," +
            "\"lng\": 113.37," +
            "\"value\": 27" +
            "}," +
            "{" +
            "\"lat\": 31.91," +
            "\"lng\": 102.23," +
            "\"value\": 27" +
            "}," +
            "{" +
            "\"lat\": 29.07," +
            "\"lng\": 119.39," +
            "\"value\": 27" +
            "}," +
            "{" +
            "\"lat\": 23.12," +
            "\"lng\": 109.6," +
            "\"value\": 27" +
            "}," +
            "{" +
            "\"lat\": 30.41," +
            "\"lng\": 81.15," +
            "\"value\": 28" +
            "}," +
            "{" +
            "\"lat\": 36.96," +
            "\"lng\": 100.91," +
            "\"value\": 28" +
            "}," +
            "{" +
            "\"lat\": 36.29," +
            "\"lng\": 100.62," +
            "\"value\": 28" +
            "}," +
            "{" +
            "\"lat\": 26.05," +
            "\"lng\": 119.18," +
            "\"value\": 28" +
            "}," +
            "{" +
            "\"lat\": 49.22," +
            "\"lng\": 119.77," +
            "\"value\": 28" +
            "}," +
            "{" +
            "\"lat\": 26.27," +
            "\"lng\": 117.64," +
            "\"value\": 29" +
            "}," +
            "{" +
            "\"lat\": 40.75," +
            "\"lng\": 107.38," +
            "\"value\": 29" +
            "}," +
            "{" +
            "\"lat\": 18.14," +
            "\"lng\": 109.31," +
            "\"value\": 29" +
            "}," +
            "{" +
            "\"lat\": 38.29," +
            "\"lng\": 109.74," +
            "\"value\": 29" +
            "}," +
            "{" +
            "\"lat\": 43.66," +
            "\"lng\": 122.25," +
            "\"value\": 30" +
            "}," +
            "{" +
            "\"lat\": 29.72," +
            "\"lng\": 118.35," +
            "\"value\": 30" +
            "}," +
            "{" +
            "\"lat\": 38.86," +
            "\"lng\": 105.732," +
            "\"value\": 30" +
            "}," +
            "{" +
            "\"lat\": 28.58," +
            "\"lng\": 118.52," +
            "\"value\": 31" +
            "}," +
            "{" +
            "\"lat\": 29.27," +
            "\"lng\": 88.89," +
            "\"value\": 31" +
            "}," +
            "{" +
            "\"lat\": 40.39," +
            "\"lng\": 109.49," +
            "\"value\": 32" +
            "}," +
            "{" +
            "\"lat\": 29.24," +
            "\"lng\": 91.78," +
            "\"value\": 33" +
            "}," +
            "{" +
            "\"lat\": 23.42," +
            "\"lng\": 113.01," +
            "\"value\": 33" +
            "}," +
            "{" +
            "\"lat\": 25.3," +
            "\"lng\": 103.48," +
            "\"value\": 33" +
            "}," +
            "{" +
            "\"lat\": 39.61," +
            "\"lng\": 109.78," +
            "\"value\": 33" +
            "}," +
            "{" +
            "\"lat\": 23.76," +
            "\"lng\": 109.23," +
            "\"value\": 34" +
            "}," +
            "{" +
            "\"lat\": 22.48," +
            "\"lng\": 108.19," +
            "\"value\": 35" +
            "}," +
            "{" +
            "\"lat\": 40.99," +
            "\"lng\": 113.14," +
            "\"value\": 35" +
            "}," +
            "{" +
            "\"lat\": 24.41," +
            "\"lng\": 111.57," +
            "\"value\": 35" +
            "}," +
            "{" +
            "\"lat\": 23.19," +
            "\"lng\": 109.24," +
            "\"value\": 35" +
            "}," +
            "{" +
            "\"lat\": 45.63," +
            "\"lng\": 112.84," +
            "\"value\": 36" +
            "}]", DatavMapNewResponse.class);

    @GetMapping(value = "/index", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<DatavIndexResponse> index() {


        TicketOrderCompanyResponse.Count count = seeRecordApiMessageMapper.selectTicketOrderCount(null);

        List<DatavIndexResponse> list = Lists.newArrayList();
        DatavIndexResponse response = DatavIndexResponse.builder()
                .value(46813)//平台商品总数
                .suffix("件")
                .build();
        list.add(response);

        PvCacheModel pvCacheModel = new PvCacheModel(redisUtil);
        Integer pv = pvCacheModel.get();
        response = DatavIndexResponse.builder()
                .value(ObjectUtil.defaultIfNull(pv, 3523))//今日线上总访客
                .suffix("pv")
                .build();
        list.add(response);

        TicketOrderSumCacheModel ticketOrderSumCacheModel = new TicketOrderSumCacheModel(redisUtil);
        Integer ticketOrderSum = ticketOrderSumCacheModel.get();
        Integer sum = count.getOrderMoney() + ObjectUtil.defaultIfNull(ticketOrderSum, 100000000);
        response = DatavIndexResponse.builder()
//                .value(sum >= 1351276566 ? 1351276566 : sum)//展会成交额
//                .value(1351276566)//展会成交额
                .value(count.getOrderMoney())//展会成交额
                .prefix("¥")
                .build();
        list.add(response);

        return list;
    }

    /**
     * 线上访客来源Top 5
     */
    @GetMapping(value = "/visit", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<DatavVisitResponse> visit() {
        List<DatavVisitResponse> list = seeRecordApiMessageMapper.selectCount();
        int i = 1;
        for (DatavVisitResponse datavVisitResponse : list) {
            datavVisitResponse.setS(String.valueOf(i++));
        }
        return list;
    }


    /**
     * 实时访客轨迹最新10条
     */
    @GetMapping(value = "/visit/new", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<DatavVisitNewResponse> visitNew() {

        PageHelper.startPage(1, 10);
        List<CrmSeeRecordModel> recordModels = seeRecordApiMessageMapper.getCrmSeeRecord();
        List<DatavVisitNewResponse> list = ConvertUtils.page2(recordModels, recordModel -> {
            UserDto user = userAdminService.get(Math.toIntExact(recordModel.getUserId()));
            String province = getProvince(user);
            String name = ReUtil.replaceAll(SpecialSymbolsUtil.replaceSymbols(user.getName()), Validator.MOBILE, "*");
            Goods goods = goodsMapper.selectByPrimaryKey(recordModel.getItmeId());
            return DatavVisitNewResponse.builder()
                    .value(province)
                    .content(String.format("%s地区的%s访客正在查看%s产品", province, name, Objects.nonNull(goods) ? goods.getGoodsName() : StringUtils.EMPTY))
                    .build();
        }).getList();

        int i = 1;
        for (DatavVisitNewResponse datavVisitResponse : list) {
            datavVisitResponse.setValue("浙江" + i++);
        }
        return list;
    }

    private String getProvince(UserDto user) {
        if (Objects.nonNull(user) && StringUtils.isNoneBlank(user.getProvince())) {
            return user.getProvince();
        }
        return "浙江";
    }

    /**
     * 今天访客 地理统计
     */
    @GetMapping(value = "/visit/map", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<DatavMapNewResponse> visitMap() {

        DatavMapNewCacheModel datavMapNewCacheModel = new DatavMapNewCacheModel(redisUtil);
        List<DatavMapNewResponse> list = datavMapNewCacheModel.getOrLoad();

        return ObjectUtil.defaultIfNull(list, default_list);
    }

    private class DatavMapNewCacheModel extends PublicBaseCacheModel<List<DatavMapNewResponse>> {

        public DatavMapNewCacheModel(RedisUtil redisUtil) {
            super(redisUtil, "datavMapNew@", 30 * 60 * 60);
        }

        @Override
        protected List<DatavMapNewResponse> getData() {
            return seeRecordApiMessageMapper.selectCountTop100();
        }
    }
}
